package com.css.fxfzaqbz.modules.data.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.AuxiliaryMeasures;
import com.css.fxfzaqbz.modules.data.queryParams.SeismicHazardAssessQueryParams;
import com.css.fxfzaqbz.modules.data.repository.AuxiliaryMeasuresRepository;
import com.css.fxfzaqbz.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AuxiliaryMeasuresRepositoryImpl implements AuxiliaryMeasuresRepository {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Override
    public Map<String, Object> getAllAuxiliaryMeasures(AuxiliaryMeasures auxiliaryMeasures) {

        StringBuilder sql = new StringBuilder("SELECT a.* from (SELECT dm.id,dm.task_name,dm.ia_status,ia.ia_number,ia.ia_id,ia.ia_name," +
                "ia.ia_activity_status,ia.create_time,ia.ia_zddw_ywry,ia.ia_xtdw_ywry,ia.ia_ssdw_ywry,ia.ia_cjr,ia.province_id,ia.del_flag FROM sys_big_dispose_manager dm left join sys_important_activity ia on dm.ia_id =ia.ia_id ) a ");

        StringBuilder orderSql = new StringBuilder(" order by a.create_time desc ,a.task_name asc");

        StringBuilder whereSql = new StringBuilder(" where a.del_flag = '" + YNEnum.N.toString() + "'");


        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getProvinceId()) && auxiliaryMeasures.getProvinceId() != "") {
            whereSql.append(" and a.province_id = '" + auxiliaryMeasures.getProvinceId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getDlrId())) {
            whereSql.append(" and a.create_user  ='" + auxiliaryMeasures.getDlrId() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getIaNumber())) {
            whereSql.append(" and a.ia_number like '%" + auxiliaryMeasures.getIaNumber() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getIaName())) {
            whereSql.append(" and a.ia_name like '%" + auxiliaryMeasures.getIaName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getIaStatus())) {
            whereSql.append("and a.ia_status = '" + auxiliaryMeasures.getIaStatus() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getTaskName())) {
            whereSql.append("and a.task_name = '" + auxiliaryMeasures.getTaskName() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(auxiliaryMeasures.getIaActivityStatus())) {
            whereSql.append("and a.ia_activity_status ='" + auxiliaryMeasures.getIaActivityStatus() + "'");
        }
//        int firstIndex = (auxiliaryMeasures.getCurPage() - 1) * auxiliaryMeasures.getPageSize();
////        int lastIndex = auxiliaryMeasures.getPageSize();
        sql.append(whereSql).append(orderSql).toString();
//        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AuxiliaryMeasures.class)).size();
        List<AuxiliaryMeasures> auxiliaryMeasuresList = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AuxiliaryMeasures.class));
        for (AuxiliaryMeasures Param : auxiliaryMeasuresList) {
            String ywrys = Param.getIaZddwYwry() + "," + Param.getIaSsdwYwry() + "," + Param.getIaXtdwYwry();
            Param.setYwrys(ywrys);
        }
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", auxiliaryMeasuresList);
        return map;
    }

    @Override
    public void updateBigStatus(String id) {
        String sql = "update sys_big_dispose_manager set" +
                " ia_status = '"+YNEnum.Y+"' " +
                " where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }
}
